var prefix = jsctx + "/printer";

var printer = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable({
                method: 'get',
                url: prefix + "/listPagePrinter",
                iconSize: 'outline',
                striped: true,
                dataType: "json",
                pagination: true,
                singleSelect: false,
                pageSize: 20,
                pageList: [20],
                pageNumber: 1,
                sidePagination: "server",
                queryParams: function (params) {
                    return {
                        limit: params.limit,
                        offset: (params.offset / params.limit) + 1,
                        priStatus: $("#priStatus").val().trim(),
                        priRtua: $("#priRtua").val().trim(),
                        elecStationId: $("#elecStationId").val().trim()
                    };
                },
                // toolbar : '#exampleToolbar',
                columns: [
                    {
                        field: 'number',
                        title: '序号',
                        align: 'center',
                        formatter: function (value, row, index) {
                            return index + 1;
                        }
                    },
                    {field: 'stationName', title: '桩群名称', align: 'center'},
                    {field: 'priRtua', title: '打印机逻辑地址', align: 'center'},
                    {field: 'priTime', title: '剩余打印次数', align: 'center'},
                    {field: 'warncount', title: '预警数', align: 'center'},
                    {
                        field: 'priStatus',
                        title: '打印机状态',
                        align: 'center',
                        valign: 'center',
                        formatter: function (value, row, index) {
                            if (row.priStatus == '1') {
                                return '<span class="label label-primary">正常</span>';
                            } else if (row.priStatus == '2') {
                                return '<span class="label label-warning">缺纸</span>';
                            } else if (row.priStatus == '3') {
                                return '<span class="label label-error">故障</span>';
                            }
                        }
                    },
                    {
                        title: '操作',
                        field: 'id',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var e = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="编辑" onclick="printer.edit(\''
                                + row.id
                                + '\')"><i class="fa fa-edit">修改</i></a> ';
                            return e;
                        }
                    }]
            });
    };

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
        load();
    };

    /**
     * 增加
     */
    function add() {
        location.href = prefix + "/add";
    };

    /**
     * 修改
     * @param id
     */
    function edit(id) {
        location.href = prefix + "/edit/" + id;
    };

    /**
     * 导出Excel
     * @param id
     */

    function exportFile(params) {
        //printSize priRtua priStatus stationName
        var printSize = $("#printSize").val().trim();
        var priRtua = $("#priRtua").val().trim();
        var epgId = $("#id").val().trim();
        var priStatus = $("#priStatus").val().trim();
        if (printSize == null || printSize == '') {
            printSize = 0;
        }
        location.href = prefix + "/exportPrinter?printSize=" + printSize +
            "&priRtua=" + priRtua + "&epgId=" + epgId + "&priStatus=" + priStatus;
    };


    /**
     * 删除
     * @param id
     */


    /**
     * 返回列表
     */
    function goBack() {
        location.href = prefix + "/index";
    };

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        add: add,
        edit: edit,
        exportFile: exportFile

    };
})();

var addprinter = (function () {
    function init() {
        validateRule();
    };

    function save() {
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/save",
            data: $('#frmAdd').serialize(),
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("保存成功");
                    printer.goBack();
                } else {
                    parent.layer.alert(data.message)
                }
            }
        });
    };

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmAdd").validate({
            rules: {
                priRtua: {
                    required: true
                },
                epgId: {
                    required: true
                },
                warncount: {
                    required: true
                },
                priTime: {
                    required: true
                }
            },
            messages: {
                priRtua: {
                    required: icon + "请输入打印机逻辑地址"
                },
                warncount: {
                    required: icon + "请输入警告提示数"
                },
                epgId:{
                    required: icon + "请选择桩群"
                },
                priTime: {
                    required: icon + "请选可打印次数"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    };

    return {
        init: init
    };
})();

var editprinter = (function () {
    function init() {
        validateRule();
    };

    function update() {
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/update",
            data: $('#frmEdit').serialize(),// 你的formid
            async: false,
            error: function (request) {
                parent.layer.alert("修改失败");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("更新成功");
                    printer.goBack();
                } else {
                    parent.layer.alert(data.message)
                }
            }
        });
    };

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmEdit").validate({
            rules: {
                priRtua: {
                    required: true
                },
                warncount: {
                    required: true
                },
                priTime: {
                    required: true
                }
            },
            messages: {
                priRtua: {
                    required: icon + "请输入打印机逻辑地址"
                },
                warncount: {
                    required: icon + "请输入警告提示数"
                },
                priTime: {
                    required: icon + "请选可打印次数"
                }
            },
            submitHandler: function (form) {
                update();
            }
        })
    };

    return {
        init: init
    };
})();